﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarLeasing.Data;

namespace CarLeasing.Models
{
    public static class Extensions
    {
        public static CarInfo[] Favorites(this User user, LocalStore store)
        {
            return store.Of<Favorite>()
                .Where(x => x.UserId == user.Id)
                .Select(x => x.CarId)
                .Select(x => store.Of<CarInfo>().Find(x))
                .ToArray();
        }

        public static void AddFavorite(this User user, CarInfo car, LocalStore store)
        {
            store.Of<Favorite>().Add(new Favorite(Guid.NewGuid(), user.Id, car.Id));
            store.Of<Favorite>().Save();
        }

        public static void RemoveFavorite(this User user, CarInfo car, LocalStore store)
        {
            var target = store.Of<Favorite>().FirstOrDefault(x => x.CarId == car.Id && x.UserId == user.Id);
            if (target == null)
                return;
            store.Of<Favorite>().Remove(target.Id);
            store.Of<Favorite>().Save();
        }
    }
}
